﻿using LY.Common.Utils;
using LY.Entity;
using LY.Entity.Entities;
using LY.Entity.Jsons;
using LY.Entity.PageSearch;
using LY.Entity.SearchEntity;
using LY.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace QJ.CMS.Areas.Admin.Controllers
{
    public class CompanyController : Controller
    {
        //
        // GET: /Admin/Company/
        private static ICompanyService _company;
        private static IMemberService _member;
        private ICompanydocumentService _companydocument;

        public CompanyController(ICompanyService company, IMemberService member, ICompanydocumentService companydocument)
        {
            _company = company;
            _member = member;
            _companydocument = companydocument;
        }

        public ActionResult Index()
        {
            return View();
        }

        //公司基本信息列表
        [HttpPost]
        public JsonResult CompanyList(CompanySearch search, FormCollection form)
        {
            PageView view = new PageView(form);
            string colkey = form["colkey"];
            string colsinfo = form["colsinfo"];
            PagedList<U_CompanyAnnex_EX> companyList = _company.QueryCompanyList(search, view);
            JsonQTable at = JsonQTable.ConvertFromPagedList<U_CompanyAnnex_EX>(companyList, colkey, colsinfo.Split(','));
            return Json(at);
        }

        //审核界面
        public ActionResult CompanyCheck(int id)
        {
            U_Company company = _company.GetCompany(id);
            List<U_Companydocument> companydocumentlist = _companydocument.GetCompanydocumentList(company.Id);//获取图片
            ViewBag.Companydocument = companydocumentlist;

            ViewBag.Company = company;
            return View();
        }

        //保存审核结果,审核成功后修改会员状态为 企业用户
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult SaveCompanyCheck(U_Company company, FormCollection collection)
        {
            JsonSMsg rMsg = new JsonSMsg();
            try
            {
                int ret = _company.SaveCopmpany(company);

                //修改会员状态为企业用户
                U_Member member = _member.getMemberInfo(company.MemberId);
                if (company.Status == 2)
                {
                    member.Classify = "企业";
                }
                else
                {
                    member.Classify = "个人";
                }

                int ret2 = _member.SaveMember(member);

                U_Companydocument img = new U_Companydocument();
                if (company.Id == 0)
                {
                    img.CompanyId = ret;
                }
                else
                {
                    img.CompanyId = company.Id;
                }
                if (ret > 0 && ret2 > 0)
                {
                    //添加公司附件信息
                    img.DocPath = collection["hid_pic"];
                    AddImages(img);
                    rMsg.Status = 1;
                    rMsg.Message = "操作成功！";
                }
                else
                {
                    rMsg.Status = -1;
                    rMsg.Message = "意外错误，请稍后重试！";
                }
            }
            catch (Exception ex)
            {
                rMsg.Status = -1;
                rMsg.Message = ex.Message;
            }
            return Json(rMsg);
        }
        #region 上传图片到本地
        public static List<ReturnParameters> rlist = new List<ReturnParameters>();
        [HttpPost]
        public ActionResult UpLoadProcess(UploadParameters para)
        {
            ReturnParameters path = new ReturnParameters();
            ActionResult json = new UploadImages("Upload", new string[]{ 
                "Upload/FuJian"
            }).UpLoadProcess(para, ref path);

            rlist.Add(path);

            return json;

        }
        //添加图片
        private int AddImages(U_Companydocument img)
        {
            List<U_Companydocument> imgList = new List<U_Companydocument>();
            int result = 0;
            if (rlist.Count > 0)
            {
                foreach (var item in rlist)//遍历容器
                {
                    U_Companydocument newimg = new U_Companydocument();

                    newimg.DocPath = item.filePath;

                    newimg.CompanyId = img.CompanyId;

                    imgList.Add(newimg);
                }
                result = _companydocument.AddImages(imgList);
            }
            return result;
        }

        #endregion
    }
}
